/*
 * @Author: Tyler
 * @Date: 2025-10-13 17:08:29
 * @Description: 
 * Copyright (c) 2025 by User/Corp, All Rights Reserved.
 */
#include "Layer.h"
#include "LayerManager.h"
#include <memory>
#include <stdexcept>

LayerManager& LayerManager::getInstance() {
    static LayerManager instance;
    return instance;
}
void LayerManager::addLayer(const shared_ptr<Layer>& layer) {
    if (layers.find(layer->getLayerIndex()) != layers.end()) {
        throw invalid_argument("layer with same id already exists");
    }
    layers[layer->getLayerIndex()] = layer;
}
shared_ptr<Layer> LayerManager::getLayerByIndex(int layerIndex) {
    if (layers.find(layerIndex) == layers.end()) {
        throw invalid_argument("layer with given Layer ID does not exist");
    }
    return layers[layerIndex];
}
int LayerManager::getNumLayers() const {
    return layers.size();
}
void LayerManager::connectLayers() {
    if (layers.empty()) {
        throw runtime_error("No layers in the network");
    }
    if (layers.size() == 1) {
        return;
    }
    layers[0]->setNextLayer(layers[1]);
    layers[layers.size() - 1]->setPrevLayer(layers[layers.size() - 2]);
    for (int i = 1; i < layers.size()-1; ++i) {
        auto layer = layers[i];
        layer->setPrevLayer(layers[i-1]);
        layer->setNextLayer(layers[i+1]);
    }
}
void LayerManager::clear() {
    layers.clear();
}